var t = getApp(), a = void 0;

Page({
    data: {
        calculation: {
            coupon: []
        },
        couponFlag: 0,
        totalPrice: 0,
        interalCheck: !1,
        litFee: [ {
            label: "没有小费",
            value: 0
        }, {
            label: "1元",
            value: 1
        }, {
            label: "2元",
            value: 2
        }, {
            label: "4元",
            value: 4
        }, {
            label: "6元",
            value: 6
        }, {
            label: "8元",
            value: 8
        }, {
            label: "10元",
            value: 10
        } ],
        litFeeFlag: 0,
        times: t.com.getTimeOptions(),
        timeFlag: 0,
        showDetail: !1
    },
    timeChange: function(t) {
        this.setData({
            timeFlag: t.detail.value
        }), a.reSumPrice();
    },
    feeDetail: function() {
        this.setData({
            showDetail: !this.data.showDetail
        });
    },
    onLoad: function(t) {
        (a = this).setData(JSON.parse(t.data)), a.sumPrice();
    },
    changeCoupon: function(t) {
        this.setData({
            couponFlag: t.detail.value,
            interalCheck: !1
        }), this.reSumPrice();
    },
    reSumPrice: function() {
        var a = parseFloat(this.data.calculation.totalPrice);
        this.data.couponFlag > 0 && (a -= parseFloat(this.data.calculation.coupon[this.data.couponFlag].coupon.discount_amount)), 
        this.data.interalCheck && (a -= parseFloat(this.data.calculation.interal.exchange)), 
        a += parseFloat(this.data.litFee[this.data.litFeeFlag].value);
        var e = -1, i = void 0;
        if (1 == this.data.calculation.timeOption.open_night) {
            var s = void 0;
            0 == this.data.timeFlag ? (s = new Date().getHours(), i = t.com.dateFormat(0, -1, "yyyy-MM-dd hh:mm:ss")) : (s = this.data.times[this.data.timeFlag].value, 
            i = this.data.times[this.data.timeFlag].label.indexOf("明天") ? t.com.dateFormat(1, s, "yyyy-MM-dd hh:mm:ss") : t.com.dateFormat(0, s, "yyyy-MM-dd hh:mm:ss"));
            for (var o in this.data.calculation.timeOption.nightOptons) this.data.calculation.timeOption.nightOptons[o] == s && (e = o);
            -1 != e && (a += parseFloat(this.data.calculation.timeOption.nightPrice));
        }
        this.setData({
            send_time: i,
            totalPrice: a.toFixed(2),
            nightPrice: -1 != e ? this.data.calculation.timeOption.nightPrice : 0
        });
    },
    sumPrice: function() {
        wx.showLoading({
            title: "加载中"
        }), t.com.post("wx/cost/calculation", {
            startAddress: JSON.stringify(this.data.startAddress),
            endAddress: JSON.stringify(this.data.endAddress),
            weight_id: "代驾" == this.data.title ? 0 : this.data.weight.id,
            service_type: this.data.title
        }, function(t) {
            wx.hideLoading(), 0 === t.errno && (a.setData({
                calculation: t.data,
                totalPrice: t.data.totalPrice
            }), a.reSumPrice());
        });
    },
    clickBox: function(t) {
        t.detail.value.length > 0 ? this.setData({
            interalCheck: !0,
            couponFlag: 0
        }) : this.setData({
            interalCheck: !1
        }), this.reSumPrice();
    },
    feeChange: function(t) {
        this.setData({
            litFeeFlag: t.detail.value
        }), this.reSumPrice();
    },
    doPay: function() {
        wx.showLoading({
            title: "加载中"
        }), t.com.post("wx/order/pay", {
            startAddress: JSON.stringify(this.data.startAddress),
            endAddress: JSON.stringify(this.data.endAddress),
            weight_id: "代驾" == this.data.title ? 0 : this.data.weight.id,
            service_type: this.data.title,
            timeValue: this.data.times[this.data.timeFlag].value,
            couponId: this.data.couponFlag > 0 ? this.data.calculation.coupon[this.data.couponFlag].id : 0,
            useInteral: this.data.interalCheck ? 1 : 0,
            textarea: this.data.textarea || "代驾",
            send_time: this.data.send_time,
            form_ids: this.data.formId.toString(),
            tip: this.data.litFee[this.data.litFeeFlag].value
        }, function(a) {
            wx.hideLoading(), 0 === a.errno && wx.requestPayment({
                timeStamp: a.data.timestamp,
                nonceStr: a.data.nonceStr,
                package: a.data.package,
                signType: "MD5",
                paySign: a.data.paySign,
                success: function(t) {
                    wx.showToast({
                        title: "下单成功",
                        duration: 1e3,
                        mask: !0
                    }), setTimeout(function() {
                        wx.redirectTo({
                            url: "/pages/order/detail/detail?order_no=" + a.data.out_trade_no
                        });
                    }, 1e3);
                },
                fail: function(e) {
                    wx.showLoading({
                        title: "取消支付中",
                        mask: !0
                    }), t.com.post("wx/orderCancel/close", {
                        order_no: a.data.out_trade_no
                    }, function(t) {
                        wx.hideLoading(), 0 === t.errno && wx.showToast({
                            title: t.errmsg
                        });
                    });
                }
            });
        });
    },
    desform: function(t) {
        this.setData({
            "formId[3]": t.detail.formId
        });
    }
});